/**
 * @author zhouheng
 * @date 2022年03月26日 20:30
 */

package com.atguigu.seckill.schedule;

import com.atguigu.seckill.service.SeckillService;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    SeckillService seckillService;

    private static final String upload_lock = "seckill:upload:lock";

    /**
     * @Author zhouheng
     * @Description 商品上架
     * @Date 20:31 2022/3/26 0026
     * @Param []
     * @return void
     **/
    @Scheduled(cron = "0 0 0-8 * * ?")
    public void uploadSeckillSkuLatest3Day(){
        // 关键位置加日志记录
        log.info("\n上架秒杀商品的信息");
        RLock lock = redissonClient.getLock(upload_lock);// "seckill:upload:lock";
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLatest3Day();
        } finally {
            lock.unlock();
        }
    }
}
